<?php

require_once('_Core.php');

class SilkCore_PageMeta_MetaDescription extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Description';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }

}

class SilkCore_PageMeta_MetaKeywords extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Keywords';
    }

    public function getInputType(){
	return Silk::INPUT_TEXT;
    }

}

class SilkCore_PageMeta_MetaTitle extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Title';
    }

    public function getInputType(){
	return Silk::INPUT_TEXT;
    }

}

class SilkCore_PageMeta_CustomPhp extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Custom PHP';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }

}

class SilkCore_PageMeta_CustomHead extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Custom Head';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }
}

class SilkCore_PageMeta_Cached extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Cached';
    }

    public function getInputType(){
	return Silk::INPUT_CHECKBOX;
    }
}

class SilkCore_PageMeta_DocType extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Doctype';
    }

    public function getInputType(){
	return Silk::INPUT_SELECT;
    }

    public function getValues(){
	return array(
	    'XHTML 1.1'			=> SILKCORE_DOCTYPE_XHTML11,
	    'XHTML 1.0 Strict' 		=> SILKCORE_DOCTYPE_XHTML10_STRICT,
	    'XHTML 1.0 Transitional' 	=> SILKCORE_DOCTYPE_XHTML10_TRANSITIONAL,
	    'XHTML 1.0 Frameset' 	=> SILKCORE_DOCTYPE_XHTML10_FRAMESET,
	    'HTML 4.01 Strict' 		=> SILKCORE_DOCTYPE_HTML4_STRICT,
	    'HTML 4.01 Transititional'	=> SILKCORE_DOCTYPE_HTML4_TRANSITIONAL,
	    'HTML 4.01 Frameset' 	=> SILKCORE_DOCTYPE_HTML4_FRAMESET
	);
    }
}

class SilkCore_PageMeta_Layout extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Layout';
    }

    public function getInputType(){
	return Silk::INPUT_SELECT;
    }

    public function getValues(){
	return '@Type=Layout Template';
    }
}

class SilkCore_PageMeta_ParentPage extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Parent Page';
    }

    public function getInputType(){
	return Silk::INPUT_SELECT;
    }

    public function getValues(){
	return '@Type=Page Template';
    }
}


class SilkCore_Type_Page extends SilkCore_Type_Base {


    public function __construct(){
	$this->_meta = array(
	    new SilkCore_PageMeta_MetaDescription(),
	    new SilkCore_PageMeta_MetaKeywords(),
	    new SilkCore_PageMeta_MetaTitle(),
	    new SilkCore_PageMeta_CustomPhp(),
	    new SilkCore_PageMeta_CustomHead(),
	    new SilkCore_PageMeta_Cached(),
	    new SilkCore_PageMeta_Doctype(),
	    new SilkCore_PageMeta_Layout(),
	    new SilkCore_PageMeta_ParentPage(),
	);
    }

    public function getName(){
	return 'Page Template';
    }

    public function getSlug(){
	return 'page';
    }

    public function getDescription(){
	return 'Page Content';
    }

    public function render(Silk_Object $pPage=null, $pAction=null, array $pVars=array()){
	$view = $this->prepare($pPage, $pVars, $pAction);

	if ($pAction instanceof Silk_Template)
	    return parent::render($pPage, $pAction, $pVars);
	else if ($pAction){
// 	    $view = $this->prepare($pPage, $pVars);

	    $action = strtolower($pAction);
	    switch ($action){
		case 'admin':
		    return $this->_admin($view);
		case 'view':
		    return $this->_view($view, $pPage);
		case 'create':
		    return $this->_create($view, $pPage);
		case 'edit':
		    return $this->_edit($view, $pPage);
		case 'delete':
		    return $this->_delete($view, $pPage);
		case 'editblock':
		    return $this->_editblock($view, $pPage);
	    }

	} else {

	    return $this->renderString($view, $pPage->content);
	}
    }

    protected function prepare(Silk_Object $pPage=null, array $pVars=array(), $pAction=null){
	$view = $this->getViewEngine();

	if ($pPage){
	    $view->assign('page', $pPage);
	    $view->assign('object', $pPage);

// 	    $view->this->appendStylesheet($this, 'css/core.manage.css');
// 	    $view->this->appendScript($this, 'js/core.manage.js');

	    $php = $pPage->getMetaValue('Custom PHP');

	    if ($php){
		$func = sprintf('silkfunc__page__%d_%d', time(), rand() % 10000);

		logdebug("Silk_Page.prepare: Evaluating custom php [$func]\n\n{$php}");

		try {
		    eval('function ' . $func . '(&$VIEW, $PARAMS, $ACTION) { ' . $php . ' }');

		    $func($view, $_REQUEST, $pAction);

		} catch (Exception $x){
		    logexception($x);
		}

		logdebug("Silk_Page.prepare: Evaluated custom php [$func]");

	    }
	}
	foreach ($pVars as $key => $val)
	    $view->assign($key, $val);

	return $view;
    }

    protected function _view(Zend_View_Interface $pView, Silk_Object $pPage){
	logdebug("Page._view: Called with page {$pPage->object_id}");
 	$layout = $pPage->getLayout();

 	$page_content = $this->renderString($pView, $pPage->content);

	if ($layout){
	    return $layout->render('view', array_merge(array('page_content' => $page_content), $pView->getVars()));
	} else
	    return $this->renderString($pView, $pPage->content);

    }

    protected function _editblock(Zend_View_Interface $pView, Silk_Object $pPage){
	require_once(dirname(__FILE__) . '/vendor/simplehtmldom/simple_html_dom.php');
	$block_id = $pView->block_id;

	$dom = str_get_html($pPage->content);
	$block = $dom->find('#' . $block_id, 0);

	logdebug('Page._editblock: block outertext - ' . $block->outertext);

	if ($_SERVER['REQUEST_METHOD'] == 'POST'){

	    if (strtolower($_POST['submit']) == 'cancel')
		$html = $block->innertext;
	    else {
		$block_content = $pView->block_content;
		logdebug('Page._editblock: New html content - ' . $block_content);

		$block->innertext = $block_content;

		$pPage->content = $dom->save();
		$pPage->save();

		$html = $block_content;
	    }
	} else {
	    logdebug('Page._editblock: Show form to edit block');
	    $html = '
<form method="post" class="inline-block-edit" action="/' . $pPage->slug . '?action=editblock&block_id=' . $block_id . '">
    <h5>Edit Content</h5>
    <textarea style="width: 99%" rows="8" name="block_content">' . $block->innertext . '</textarea>
    <input type="submit" name="submit" value="Cancel"/> <input type="submit" name="submit" value="Update"/>
</form>
	';

	}


	return $html;
    }

    protected function _create(Zend_View_Interface $pView, Silk_Object $pPage){

	if (array_key_exists('parent', $_REQUEST)){
	    if (is_numeric($_REQUEST['parent']))
		$parent = Silk::GetObject($_REQUEST['parent']);
	    else
		$parent = Silk::GetObject( array('slug' => $_REQUEST['parent']), 'page' );

	    if ($parent){
		$pPage->parent_id = $parent->object_id;
		$pView->page = $pPage;
	    }

	}

	$pView->this->appendStylesheet($this, 'css/core.manage.css');

	return $this->renderTemplate($pView, 'edit-page');

    }

    protected function _edit(Zend_View_Interface $pView, Silk_Object $pPage){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'edit-page');

    }

    protected function _admin(Zend_View_Interface $pView){
	if (array_key_exists('sort', $_REQUEST)){
	    $sort = $_REQUEST['sort'];
	} else
	    $sort = 'object_id';

	if (array_key_exists('order', $_REQUEST)){
	    $order = $_REQUEST['order'];
	} else
	    $order = 'ASC';

	if (array_key_exists('limit', $_REQUEST)){
	    $limit = $_REQUEST['limit'];
	} else
	    $limit = 50;

	if (array_key_exists('page', $_REQUEST)){
	    $page = $_REQUEST['page'];
	} else
	    $page = 1;

	$paginator = Silk::getObjectPaginator($this->getSlug(), array(), $sort, $order);

	$paginator->setItemCountPerPage($limit);
	$paginator->setCurrentPageNumber($page);

	$Types = new Silk_Types();
	$type = $Types->fetchBySlug($this->getSlug(), true);

	$pView->moduletype = $this;
	$pView->type = $type;

	$pView->pages = $paginator;
	$pView->limit = $limit;
	$pView->sort  = $sort;
	$pView->order = $order;
	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'page-admin');
    }

    /* Magic Methods */

    public function getLayout(Silk_Object $pPage, $pArgs=null){
	logdebug("Page.getLayout: Called for page {$pPage->object_id}");

	$layout_id = $pPage->getMetaValue('Layout');
	logdebug("Page.getLayout: Fetching layout {$layout_id}");
 	return Silk::GetObject($pPage->getMetaValue('Layout'));
    }

    public function getChildPages(Silk_Object $pPage, $pArgs=null){
	return Silk::GetObjects(array('parent_id' => $pPage->object_id), $this->getSlug());
    }

}

?>